#include "StdAfx.h"
#include "Executor.h"

void Executor::LimitUndo()
{
    if ( undo.size() > undoDepth )
    {
        undo.pop_back();
    }
}

bool Executor::Do( const Command& command )
{
    CommandPtr cmd = CommandPtr(command.Clone());

    try
    {
        cmd->Do();

        undo.push_front( cmd );
        redo.clear();
        LimitUndo();
    }
    catch( CommandException ) {
        return false;
    }

    return true;
}

bool Executor::Undo()
{
    ASSERT(undo.size());

    try {
        CommandPtr &cmd = undo.front();
        cmd->Undo();

        redo.push_front( cmd );
        undo.pop_front();
    }
    catch ( CommandException ){
        return false;
    }

    return true;
}

bool Executor::Redo()
{
    ASSERT(redo.size());

    try {
        CommandPtr &cmd = undo.front();
        cmd->Do();

        undo.push_front( cmd );
        redo.pop_front();
    }
    catch ( CommandException ) {
        return false;
    }

    return true;
}

bool Executor::CanUndo() {
    return undo.size() != 0;
}

bool Executor::CanRedo() {
    return redo.size() != 0;
}

std::wstring Executor::GetUndoName(){
    ATLASSERT(CanUndo());
    return undo.front()->GetName();
}

std::wstring Executor::GetRedoName() {
    ATLASSERT(CanRedo());
    return redo.front()->GetName();
}
